package cn.xgj.sfzy.controller;

import cn.xgj.sfzy.entity.Article;
import cn.xgj.sfzy.mapper.ConsultMapper;
import cn.xgj.sfzy.service.ArticleService;
import cn.xgj.sfzy.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ArticleController
 * @Description TODO
 * @Author xgj
 * @Date 2024年10月22日 下午7:36
 * @Version 1.0
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/findAll")
    public Result findAll(Article article){
        List<Article> list = articleService.findAll(article);
        return Result.success(list);
    }
    @RequestMapping("/findByPage")
    public Result findByPage(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "3") Integer pageSize,
            Article article){
        PageInfo<Article> byPage = articleService.findByPage(pageNum, pageSize, article);
        return Result.success(byPage);
    }

    @PostMapping
    public Result add(@RequestBody Article article){
        int num = articleService.add(article);
        if(num!=0){
            return Result.success("添加成功");
        }else{
            return Result.error("添加失败");
        }
    }

    @PutMapping
    public Result update(@RequestBody Article article){
        int num = articleService.edit(article);
        if(num!=0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }
    }

    @GetMapping("/{articleId}")
    public Result findById(@PathVariable("articleId") Integer articleId){
        Article article = articleService.findById(articleId);
        return Result.success(article);
    }


    @DeleteMapping("/{articleId}")
    public Result delete(@PathVariable(value = "articleId") Integer articleId){
        int num = articleService.remove(articleId);
        if(num!=0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }
}
